<!DOCTYPE html>
<html>
<head>
	<title>demo</title>
</head>
<body>
<script type="text/javascript">
	var arr = [12,3,42,44,23,13,6,4,6];
	function maopao (arr) {
		console.time('改进前冒泡排序耗时')
		var i = arr.length - 1;
		while ( i > 0){
			var pos = 0;
			for(var j = 0; j < i; j++)
				if (arr[j] > arr[j+1]) {
					pos = j;
					var tmp = arr[j];
					arr[j] = arr[j+1];
					arr[j+1] = tmp
				}
			i = pos;
		}
		console.timeEnd('改进前冒泡排序耗时');
		return arr;
		
	}

	console.log(maopao(arr))

// 改进后是改进前2倍速

	function maopao2 (arr) {
		console.time('改进后冒泡排序耗时')
		for(var i = arr.length - 1; i > 0; i -- ) {
			for( var j = 0; j < i; j++){
				if(arr[j] > arr[j+1]) {
					var tmp = arr[j];
					arr[j] = arr[j+1];
					arr[j+1] = tmp
				}
				console.timeEnd('改进后冒泡排序耗时');
				 return arr
			}
		}
	}
	
	
	console.log(maopao2(arr))


</script>
</body>
</html>